package com.scwang.smart.drawer.exercise.adapter

import android.annotation.SuppressLint
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.drawer.app.R
import com.scwang.smart.drawer.exercise.model.Example
import com.scwang.smart.drawer.exercise.model.Item
import com.scwang.smart.drawer.exercise.model.Subject

open class ExercisePagerChoice(itemView: View, example: Example) : ExerciseHolder(itemView) {
    
    private val mExample: Example = example
    
    private val mWebSubjectView: WebView = itemView.findViewById(R.id.pager_choice_subject)
    private val mRecyclerItems: RecyclerView = itemView.findViewById(R.id.pager_choice_content)
    private val mTextTitle: TextView = itemView.findViewById(R.id.pager_choice_title)
    private val mTextChapter: TextView = itemView.findViewById(R.id.pager_choice_chapter)
    private val mTextIndicator: TextView = itemView.findViewById(R.id.pager_choice_indicator)
    private val mTextSubject: TextView = itemView.findViewById(R.id.pager_choice_subject_text)
    
    constructor(parent: ViewGroup, example: Example) : this(LayoutInflater.from(parent.context).inflate(R.layout.holder_exercise_pager_group, parent, false), example)

    private lateinit var mAdapter: ItemAdapter

    init {
        val settings = mWebSubjectView.settings
        settings.javaScriptEnabled = false
        settings.setAppCacheEnabled(false)
        settings.databaseEnabled = false
        settings.domStorageEnabled = false
        settings.setGeolocationEnabled(false)
        mRecyclerItems.setHasFixedSize(true)
        mRecyclerItems.isNestedScrollingEnabled = false
        mRecyclerItems.layoutManager = object : LinearLayoutManager(itemView.context) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
    }

    override fun onBind(subject: Subject, position: Int) {
        mTextTitle.text = mExample.Title
        mTextChapter.text = subject.ChapterName
        mTextChapter.visibility = if(subject.ChapterName.isNullOrEmpty()) View.GONE else View.VISIBLE
        mTextIndicator.text = Html.fromHtml("<big>${subject.SortNo}</big><font color='#777777'>/${mExample.Count}</font>")

        mAdapter = ItemAdapter(subject, subject.items, position)
        mRecyclerItems.adapter = mAdapter

        val html: String = subject.Content
        val matcher = matcherHtmlText(mPatternSubject, html)

        matcherHtmlText(mPatternSubject, html)
        if (matcher.find()) {
            mWebSubjectView.visibility = View.GONE
            val content: String = bindTag(R.id.pager_choice_tag, matcher.group(1)) ?: ""
            mTextSubject.visibility = View.VISIBLE
            mTextSubject.text = Html.fromHtml(content)
        } else {
            mWebSubjectView.loadData(html, "text/html", null);
            mTextSubject.visibility = View.GONE
            itemView.findViewById<View>(R.id.pager_choice_tag).visibility = View.GONE
        }
    }

    class SelectItem(parent: ViewGroup, private val mSubject: Subject, val mExamIndex: Int) : RecyclerView.ViewHolder(
        LayoutInflater.from(parent.context)
            .inflate(R.layout.holder_exercise_choice_item, parent, false)
    ) {
        private val mWebView: WebView = itemView.findViewById(R.id.choice_item_content)
        private val mTextIndex: TextView = itemView.findViewById(R.id.choice_item_index)
        private val mTextText: TextView = itemView.findViewById(R.id.choice_item_text)

        init {
            val settings = mWebView.settings
            settings.javaScriptEnabled = false
            settings.setAppCacheEnabled(false)
            settings.databaseEnabled = false
            settings.domStorageEnabled = false
            settings.setGeolocationEnabled(false)
            mWebView.isSaveEnabled = false
            mWebView.isScrollbarFadingEnabled = false
            mWebView.isSoundEffectsEnabled = false
            mWebView.isHorizontalScrollBarEnabled = false
            mWebView.isScrollbarFadingEnabled = false
            mWebView.isVerticalScrollBarEnabled = false
        }

        @SuppressLint("SetTextI18n")
        fun onBind(item: Item, index: Int) {
            val html: String = item.Content
            val matcher = matcherHtmlText(mPatternItem, html)
            if (matcher.find()) {
                mWebView.visibility = View.GONE
                mTextText.visibility = View.VISIBLE
                mTextText.text = Html.fromHtml(matcher.group(1))
            } else {
                mTextText.visibility = View.GONE
                mWebView.visibility = View.VISIBLE
                mWebView.loadData(html, "text/html", null)
            }
            mTextIndex.text = "" + ('A'.toInt() + index).toChar()
        }
    }

    class ItemAdapter(private val mSubject: Subject,private val mItems: List<Item>, private val mExamIndex:Int) : RecyclerView.Adapter<SelectItem>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SelectItem {
            return SelectItem(parent, mSubject, mExamIndex)
        }

        override fun getItemCount(): Int {
            return mItems.size
        }

        override fun onBindViewHolder(holder: SelectItem, position: Int) {
            holder.onBind(mItems[position], position)
        }
    }

}